package com.ciaojian.store.controller;

import com.ciaojian.core.util.MD5Utils;
import com.ciaojian.core.util.SecretHelper;
import com.ciaojian.miniapp.api.WxMiniAppApi;
import com.ciaojian.miniapp.model.request.Code2Session;
import com.ciaojian.miniapp.model.response.Code2SessionResponse;
import com.ciaojian.store.service.FranchiseesServiceImpl;
import com.ciaojian.core.util.Result;
import com.ciaojian.store.model.dto.FranchiseesDTO;
import io.swagger.annotations.*;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;

/**
 * @author Atlakyn
 */
@Api(tags = "登录")
@RestController
@RequestMapping("/login")
public class LoginController {
    @Resource
    private FranchiseesServiceImpl franchiseesService;
    @Value("${wx.bean.appId}")
    private String appid;
    @Value("${wx.bean.appSecret}")
    private String secret;

    @ApiOperation("账号密码登录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "account",value = "账号",paramType = "form"),
            @ApiImplicitParam(name = "password",value = "密码",paramType = "form")
    })
    @PostMapping("/account")
    public Result loginByAccount(@NotNull(message = "账号") String phone,
                                 @NotNull(message = "密码") String password) {


        return Result.ok(franchiseesService.loginByAccount(phone, password));
    }

    @ApiOperation("账号密码注册")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "account",value = "账号",paramType = "form"),
            @ApiImplicitParam(name = "password",value = "密码",paramType = "form")
    })
    @PostMapping("/register")
    public Result register(@NotNull(message = "账号") String phone,
                           @NotNull(message = "密码") String password) {
        return Result.ok(franchiseesService.register(phone, password));
    }


    @ApiOperation("手机号登录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "phone"),
            @ApiImplicitParam(name = "openId")
    })
    @PostMapping("/phone")
    public Result loginByPhone(@RequestParam("phone") String phone, @RequestParam(value = "openId", required = false) String openId) {
        Assert.hasText(phone, "请输入手机号");
        FranchiseesDTO franchiseesDTO = franchiseesService.login(phone, openId);
        return Result.ok(franchiseesDTO);
    }


    /**
     * 通过code获取openid
     */
    @ApiOperation("获取 openid")
    @GetMapping("/userOpenId")
    public Result userOpenId(@ApiParam("客户端获取的code") String code) {
        Assert.hasText(code, "code不能为空");
        Assert.hasText(code, "encryptedData不能为空");
        Assert.hasText(code, "iv不能为空");
        try {
            Code2Session code2Session = Code2Session.builder()
                    .appid(appid)
                    .secret(secret)
                    .js_code(code)
                    .build();
            Code2SessionResponse response = WxMiniAppApi.code2Session(code2Session, Code2SessionResponse.class);
            return Result.ok(response);
        } catch (Exception e) {
            return Result.error("系统异常");
        }
    }
}


